package cn.ibizlab.odoo.core.odoo_base.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Ires_groups;
import cn.ibizlab.odoo.core.odoo_base.domain.Res_groups;
import cn.ibizlab.odoo.core.odoo_base.filter.Res_groupsSearchContext;
import cn.ibizlab.odoo.core.odoo_base.service.IRes_groupsService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[访问群] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Res_groupsServiceImpl implements IRes_groupsService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Ires_groupsClientService res_groupsClientService;

    @Override
    public Res_groups get(Integer id) {
        Ires_groups clientModel = res_groupsClientService.createModel();
        clientModel.setId(id);
        res_groupsClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Res_groups get(Res_groups res_groups) {
        Res_groups retres_groups = this.get(res_groups.getId()) ;
    	BeanCopier copier=BeanCopier.create(retres_groups.getClass(), res_groups.getClass(), false);
        copier.copy(retres_groups, res_groups, null);
        return res_groups ;
    }

    @Override
    public void updateBatch(List<Res_groups> res_groupss, int batchSize) {

        //res_groupsClientService.updateBatch(res_groupss,batchSize) ;

    }

    @Override
    public boolean update(Res_groups res_groups) {
        boolean bOk = false ;
        Ires_groups clientModel = this.convert2Model(res_groups,null);
        res_groupsClientService.update(clientModel) ;
        this.convert2Domain(clientModel,res_groups) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void createBatch(List<Res_groups> res_groupss, int batchSize) {

                        //res_groupsClientService.createBatch(res_groupss,batchSize) ;

    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Ires_groups clientModel = res_groupsClientService.createModel();
        clientModel.setId(id);
        res_groupsClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //res_groupsClientService.removeBatch(res_groupss) ;

    }

    @Override
    public boolean create(Res_groups res_groups) {
        boolean bOk = false ;
        Ires_groups clientModel = this.convert2Model(res_groups,null);
        res_groupsClientService.create(clientModel) ;
        this.convert2Domain(clientModel,res_groups);
        bOk = true ;

        return bOk ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Res_groups> searchDefault(Res_groupsSearchContext context) {
        List<Res_groups> list = new ArrayList<Res_groups>();
        Page<cn.ibizlab.odoo.core.client.model.Ires_groups> clientModelList = res_groupsClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ires_groups clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Res_groups>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据用户查询
     */
    @Override
    public Page<Res_groups> selectByOdoo_create(Integer id){
        Res_groupsSearchContext context = new Res_groupsSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Res_groups> list = new ArrayList<Res_groups>();
        Page<cn.ibizlab.odoo.core.client.model.Ires_groups> clientModelList = res_groupsClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ires_groups clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Res_groups>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Res_groups> selectByOdoo_write(Integer id){
        Res_groupsSearchContext context = new Res_groupsSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Res_groups> list = new ArrayList<Res_groups>();
        Page<cn.ibizlab.odoo.core.client.model.Ires_groups> clientModelList = res_groupsClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ires_groups clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Res_groups>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Ires_groups convert2Model(Res_groups domain , Ires_groups model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = res_groupsClientService.createModel() ;
        }

         if(domain.getUsersDirtyFlag())
            model.setUsers(domain.getUsers());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getImplied_idsDirtyFlag())
            model.setImplied_ids(domain.getImplied_ids());
         if(domain.getTrans_implied_idsDirtyFlag())
            model.setTrans_implied_ids(domain.getTrans_implied_ids());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getView_accessDirtyFlag())
            model.setView_access(domain.getView_access());
         if(domain.getRule_groupsDirtyFlag())
            model.setRule_groups(domain.getRule_groups());
         if(domain.getModel_accessDirtyFlag())
            model.setModel_access(domain.getModel_access());
         if(domain.getCategory_idDirtyFlag())
            model.setCategory_id(domain.getCategory_id());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getMenu_accessDirtyFlag())
            model.setMenu_access(domain.getMenu_access());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getCommentDirtyFlag())
            model.setComment(domain.getComment());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getShareDirtyFlag())
            model.setShare(domain.getShare());
         if(domain.getColorDirtyFlag())
            model.setColor(domain.getColor());
         if(domain.getFull_nameDirtyFlag())
            model.setFull_name(domain.getFull_name());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
        return model ;
    }

    public Res_groups convert2Domain( Ires_groups model ,Res_groups domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Res_groups();
        }

        if(model.getUsersDirtyFlag())
            domain.setUsers(model.getUsers());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getImplied_idsDirtyFlag())
            domain.setImplied_ids(model.getImplied_ids());
        if(model.getTrans_implied_idsDirtyFlag())
            domain.setTrans_implied_ids(model.getTrans_implied_ids());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getView_accessDirtyFlag())
            domain.setView_access(model.getView_access());
        if(model.getRule_groupsDirtyFlag())
            domain.setRule_groups(model.getRule_groups());
        if(model.getModel_accessDirtyFlag())
            domain.setModel_access(model.getModel_access());
        if(model.getCategory_idDirtyFlag())
            domain.setCategory_id(model.getCategory_id());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getMenu_accessDirtyFlag())
            domain.setMenu_access(model.getMenu_access());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getCommentDirtyFlag())
            domain.setComment(model.getComment());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getShareDirtyFlag())
            domain.setShare(model.getShare());
        if(model.getColorDirtyFlag())
            domain.setColor(model.getColor());
        if(model.getFull_nameDirtyFlag())
            domain.setFull_name(model.getFull_name());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        return domain ;
    }


}
